this.name                                        = "OSEhiredGuns_escort";
this.author                                        = "Thargoid, modified by Lestradae";
this.copyright                                = "Creative Commons: attribution, non-commercial, sharealike.";
this.description                                = "AI-actions script for the escorts to the player ship";
this.version                                = "1.0";

this.locatePlayer = function()
        {
        this.playerArray = system.shipsWithPrimaryRole("player");
        if(this.playerArray.length > 0)
                {
                this.ship.target = this.playerArray[0];
                this.ship.reactToAIMessage("PLAYER_FOUND");
                }
        }

this.checkPlayerDistance = function()
        {
        if(!player.ship)
                {
                return;
                }

        this.playerDistance = this.ship.position.distanceTo(player.ship.position);
        if(this.playerDistance > 51200) // if the escort is more than 2 scanner ranges from the player, move it just off-scanner
                {
                let playerDirection = player.ship.position.subtract(this.ship.position).direction();
                let newPosition = player.ship.position.subtract(playerDirection.multiply(30000));
                this.ship.position = newPosition;
                this.ship.reactToAIMessage("PLAYER_MID");
                return;
                }

        if(this.playerDistance < 25600)
                {
                this.ship.reactToAIMessage("PLAYER_NEAR");
                }
        else
                {
                if(this.playerDistance > 38400 || player.ship.speed > 2000)
                        {
                        this.ship.reactToAIMessage("PLAYER_FAR");
                        }
                else
                        {
                        this.ship.reactToAIMessage("PLAYER_MID");
                        }
                }
        }

this.combatCheck = function()
        {
        this.playerDistance = this.ship.position.distanceTo(player.ship.position);
        if(this.playerDistance > 51200) // if the escort is more than 2 scanner ranges from the player, break off combat
                {
                this.ship.AIState = "LOCATE_PLAYER";
                }
        else
                {
                this.ship.reactToAIMessage("ENEMY_FIRE");
                }
        }

this.findPlayerHostiles = function()
        {
        function isHostileToPlayer(entity)
                {
                return (entity.isThargoid || (entity.isShip && entity.target && entity.target == player.ship && entity.hasHostileTarget));
                }
        if(this.ship.target == player.ship && player.ship.target && player.ship.target.bounty > 2)
                {
                this.ship.target = player.ship.target;
                this.ship.reactToAIMessage("HOSTILE_FOUND");
                }
        else
                {
                var targets = system.filteredEntities(this, isHostileToPlayer, this.ship, this.ship.scannerRange);
                if(targets.length > 0)
                        {
                        this.ship.target = targets[0];
                        this.ship.reactToAIMessage("HOSTILE_FOUND");
                        }
                else
                        {
                        this.ship.reactToAIMessage("NO_HOSTILE_FOUND");
                        }
                }
        }

this.fireCheck = function()
        {
        if((this.ship.target.hasRole("OSEhiredGuns_escort")) || (player.ship.target == this.ship && this.ship.target.isPlayer))
                {
                this.ship.reactToAIMessage("FRIENDLY_FIRE");
                }
        else
                {
                this.ship.reactToAIMessage("ENEMY_FIRE");
                }
        }

this.shipDestroyedTarget = function(target)
        {
        player.score += 1;
        player.credits += target.bounty;
        player.consoleMessage("Ship Computer Message: Escort kill - " + target.name + " : " + target.bounty + "₢ awarded.", 5);
        log("Ship Computer Message: Escort kill - " + target.name + " : " + target.bounty);
        }

this.switchID = function()
        {
        this.ship.switchAI("route1patrolAI.plist");
        this.ship.displayName = "Bounty Hunter";
        this.ship.primaryRole = "hunter";
        log("Hired escort switched to bounty hunter role");
        }

this.shipDied = function(whom,why)
        {
        if(whom && whom.isValid)
                {
                player.commsMessage("Ship Computer Emergency Message: One of your escorts was destroyed, Commander.", 6);
                }
        missionVariables.OSEhiredGuns_count -= 1;
        }